program Burgers
implicit none
    integer c_case,c_rk,c_space,c_flux
    integer mx,bl,i
    integer inputid
    integer nstep,interval

    real(8),allocatable::x(:)
    real(8),allocatable::Cu(:)
    real(8),allocatable::acc_Cu(:)

    real(8) dt,dx,tt,tend,eps,eqa
    real(8) cfln
    real(8) tSave
    integer iSave
    
    character(len=8) input !input file
    character(len=19) string0
    character(len=23) string
    character(len=30) stringi

    namelist/grids/mx,bl
    namelist/chose/c_rk,c_case,tend,c_flux,c_space,eps,cfln

    inputid=11

    write(input,'(a8)') 'data.nml'
    write(*,*) input

    open(unit=inputid,file=input,status='old')
        read(inputid,nml=grids)
        read(inputid,nml=chose)
    close(inputid)
    write(*,nml=grids)
    write(*,nml=chose)

    allocate(x(-bl:mx+bl))
    allocate(Cu(-bl:mx+bl))
    allocate(acc_Cu(-bl:mx+bl))

    tt=0.d0
    nstep=0
    
    !init(mx,my,bl,c_case,dx,dy,tend,x,y,Cu)
    call init(mx,bl,c_case,dx,tend,x,Cu,acc_Cu)
    !dt=cfln*min(dx,dy)
!    dt=dx*dx

    ! dt=dx**(5.d0/4.d0)
    write(string0,'(a4,i4,a4)') 'init',c_case,'.dat'
    call output(mx,bl,x,Cu,string0,19,tt)
    write(string0,'(a4,i4,a4)') 'accu',c_case,'.dat'
    call output(mx,bl,x,acc_Cu,string0,19,tt)
    !call output(mx,my,bl,x,y,Cu,'init.dat',8,tt)
    !call cfl(mx, my, bl, c_case, x, y, Cu, dx, dy, dt, cfln)

    write(stringi,'(a6,i4,a1,f10.5,a4)')'ForTi_',c_case,'_',tt,'.dat'      
    call output(mx,bl,x,Cu,stringi,30,tt)
    call cfl(mx, bl, c_case, x, Cu, dx, dt, cfln)
    do while(tt<tend)
        call cfl(mx, bl, c_case, x, Cu, dx, dt, cfln)
      tt=tt+dt
      if(tt>tend) then
          tt=tt-dt
          dt=tend-tt
          tt=tend
      endif
      nstep=nstep+1
      write(*,*) c_case,nstep,dt,tt

      select case(c_rk)
      case(1)
          !rk1(mx,my,bl,c_case,c_space,c_flux,eps,eqa,dt,dx,dy,x,y,Cu)
          call rk1(mx,bl,c_case,c_space,c_flux,eps,dt,dx,x,Cu)
!        case(2)
!            call rk2(mx,bl,c_case,c_space,eps,eqa,dt,dx,x,Cu)
      case(3)
          call rk3(mx,bl,c_case,c_space,c_flux,eps,dt,dx,x,Cu)
          !do j=1,my
          !    do i=0,mx
          !      print*,i,j,Cu(i,j)
          !    enddo
          !enddo
!        case(4)
!            call rk4(mx,bl,c_case,c_space,eps,eqa,dt,dx,x,Cu)

      end select
      
      !interval=10**(INT(ABS(DLOG10(dt)))-1)

       !if (mod(nstep, interval) == 0) then
       !     write(stringi,'(a6,i4,a1,f10.5,a4)')'ForTi_',c_case,'_',tt,'.dat'
       !     call output(mx,my,bl,x,y,Cu,stringi,30,tt)
       !endif
    enddo
    write(stringi,'(a6,i4,a1,f10.5,a4)')'ForTi_',c_case,'_',tt,'.dat'
    call output(mx,bl,x,Cu,stringi,30,tt)

    write(string,'(a3,i4,a1,i4,a1,i6,a4)') 'tec',c_case,'-',c_space,'-',mx,'.dat'
    call output(mx,bl,x,Cu,string,23,tt)
    
    write(string,'(a4,i4,a1,i4,a4)') 'norm',c_case,'-',c_space,'.dat'
    call outnorm(mx,bl,x,Cu,acc_Cu,string,23)
    print*,'Ending'
    
end program Burgers



